package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.domain.Wish;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IWishService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wish")
public class WishController {

    @Autowired
    private IWishService wishService;
    @GetMapping("/{userId}")
    public PageInfo listQuery(@PathVariable Long userId,QueryObject qo){
        qo.setUserId(userId);
        qo.setOrderBy("t.lastUpdateTime desc");
        return wishService.selectAll(qo);
    }

    @PostMapping("/{id}")
    public JSONResult save(@PathVariable Long id,Wish wish){
        User u = new User();
        u.setId(id);
        wish.setUser(u);
        wishService.saveOrupdate(wish);
        return new JSONResult();
    }
    @PostMapping
    public JSONResult update(Wish wish){
        if(wish.getId()==null){
            wish.setUser(UserContext.getCurrentUser());
            wishService.saveOrupdate(wish);
        }
        return new JSONResult();
    }

    @GetMapping("/{id}/wish")
    public Wish selectWishById(@PathVariable Long id){
        return wishService.selectWishById(id);
    }
}
